package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.Tag;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.TagDaoLocal;

@SuppressWarnings("serial")
public class DeleteTagController extends HttpServlet {
	
	private static Logger log = Logger.getLogger(DeleteTagController.class);

	
	@EJB
	PhotoDaoLocal photoDao;
	
	@EJB
	TagDaoLocal tagDao;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		int photoId = Integer.parseInt(request.getParameter("photoId"));
		int tagId = Integer.parseInt(request.getParameter("tagId"));
		
		Photo photo = photoDao.findById(photoId);
		Tag tag = tagDao.findById(tagId);
		tag.getTaggedPhotos().remove(photo);		
		tagDao.merge(tag);
		
		log.info(new Date() + " uklonjen tag[id]:" + tagId);
		response.sendRedirect("./ViewPhotoController?id=" + photoId);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
